<template>
    <Teleport to="body">
        <transition name="modal">
            <div v-if="visible" class="modal-mask">
                <div class="modal-wrapper">
                    <div class="modal-container">

                    <div class="modal-header">
                        <slot name="header">
                            <div style="position: relative;">
                                <span style="font-size: 18px;">header</span>
                                <button aria-label="close" class="el-dialog__headerbtn" type="button" @click.stop="closeDialog">
                                    <i class="el-icon el-dialog__close">
                                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024"><path fill="currentColor" d="M764.288 214.592 512 466.88 259.712 214.592a31.936 31.936 0 0 0-45.12 45.12L466.752 512 214.528 764.224a31.936 31.936 0 1 0 45.12 45.184L512 557.184l252.288 252.288a31.936 31.936 0 0 0 45.12-45.12L557.12 512.064l252.288-252.352a31.936 31.936 0 1 0-45.12-45.184z"></path>
                                        </svg>
                                    </i>
                                </button>
                            </div>
                        </slot>
                    </div>

                    <div class="modal-body">
                        <slot name="body">
                        default body
                        </slot>
                    </div>

                    <div class="modal-footer">
                        <slot name="footer">
                        default footer
                        <button class="modal-default-button" @click.stop="closeDialog">
                            OK
                        </button>
                        </slot>
                    </div>
                    </div>
                </div>
            </div>
        </transition>
    </Teleport>
</template>

<script lang="ts">

import { defineComponent,ref,onMounted,watch } from "vue";

export default defineComponent({
    props:{
        title:{
          type:String,
          default:"Dialog"
        },
        modelValue:{
          type:Boolean,
          default:false
        },
    },
    emits: ['update:modelValue'],
    setup(props,context){
        let visible = ref(false)
        watch(
            () => props.modelValue,
            (count, prevCount) => {
                visible.value = count
            }
        )
        let closeDialog = ()=>{
            visible.value = !visible.value
            context.emit('update:modelValue', visible.value)
        }
        return {
            visible,
            closeDialog
        }
    }
})

</script>

<style scoped>

.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .5);
  display: table;
  transition: opacity .3s ease;
}
.modal-wrapper {
  display: table-cell;
  vertical-align: middle;
}
.modal-container {
  width: 50%;
  margin: 0px auto;
  padding: 20px 30px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
  transition: all .3s ease;
}
.modal-header h3 {
  margin-top: 0;
  color: #42b983;
}
.modal-body {
  margin: 20px 0;
  overflow-y: auto;
  max-height: 70vh;
}
.modal-default-button {
  float: right;
}

.modal-enter-from {
  opacity: 0;
}
.modal-leave-to {
  opacity: 0;
}
.modal-enter-from .modal-container,
.modal-leave-to .modal-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

.el-dialog__headerbtn {
    position: absolute;
    top: -10px;
    right: -10px;
    padding: 0;
    background: 0 0;
    border: none;
    outline: 0;
    cursor: pointer;
    font-size: 16px;
}
</style>